package com.mz.ozy.activity.me;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mz.ozy.R;
import com.mz.ozy.activity.c2c.PhotoScanActivity;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.CarGoodsBean;
import com.mz.ozy.bean.CarGoodsDetailBean;
import com.mz.ozy.bean.CartListBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.ReturnPresenter;
import com.mz.ozy.mvp.view.RetunContract;
import com.mz.ozy.utils.TimeUtils;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.utils.Utils;
import com.mz.ozy.utils.glideUtils.GlideUtils;
import com.mz.ozy.widget.dialog.BaseDialog;

import org.json.JSONObject;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lifeStyle on 2020/4/17 0017.
 */
public class ReturnGoodsListDetailsActivity extends BaseActivity<RetunContract.View, ReturnPresenter> implements RetunContract.View {
    @BindView(R.id.tv_order_state)
    TextView tvOrderState;
    @BindView(R.id.tv_order_no)
    TextView tvOrderNo;
    @BindView(R.id.iv_icon)
    ImageView ivIcon;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_info)
    TextView tvInfo;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_count)
    TextView tvCount;
    @BindView(R.id.tv_goods_count)
    TextView tvGoodsCount;
    @BindView(R.id.tv_money1)
    TextView tvMoney1;
    @BindView(R.id.tv_freight)
    TextView tvFreight;
    @BindView(R.id.ll_change)
    LinearLayout llChange;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_version)
    TextView tvVersion;
    @BindView(R.id.tv_speak)
    TextView tvSpeak;
    @BindView(R.id.image_one)
    ImageView imageOne;
    @BindView(R.id.image_two)
    ImageView imageTwo;
    @BindView(R.id.image_three)
    ImageView imageThree;
    @BindView(R.id.image_grid)
    LinearLayout imageGrid;
    @BindView(R.id.tv_order_pay_time)
    TextView tvOrderPayTime;
    @BindView(R.id.tv_order_over_time)
    TextView tvOrderOverTime;
    @BindView(R.id.line)
    LinearLayout line;
    @BindView(R.id.ll_visi)
    LinearLayout llVisi;
    @BindView(R.id.ll_wu)
    LinearLayout ll_wu;
    @BindView(R.id.et_wu_name)
    EditText etWuName;
    @BindView(R.id.et_wl_num)
    EditText etWlNum;
    @BindView(R.id.tv_tj)
    TextView tv_tj;
    @BindView(R.id.tv_order_success)
    TextView tv_order_success;
    @BindView(R.id.tv_return_voucher)
    TextView tvReturnVoucher;
    private CarGoodsDetailBean dataItem;
    private int countDownSec;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                countDownSec--;
                if (countDownSec > 0) {
                    int totalSeconds = countDownSec;
                    int minute = 0;
                    int hour = 0;
                    int day = 0;
                    if (totalSeconds >= 60) {

                        minute = totalSeconds / 60;
                        totalSeconds = totalSeconds % 60;
                        if (minute >= 60) {

                            hour = minute / 60;
                            minute = minute % 60;
                            if (hour > 24) {

                                day = hour / 24;
                                hour = hour % 24;
                            }
                        }
                    }


                    tvOrderNo.setText("还剩" + day + "天" + Utils.formatHMS(hour, minute, totalSeconds));
                    mHandler.sendEmptyMessageDelayed(1, 1000);
                } else {

                    mPresenter.onlistDetail(ReturnGoodsListDetailsActivity.this, getIntent().getStringExtra("id"));
                }

            }
        }
    };
    private String id;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_return_list_detail);
        ButterKnife.bind(this);
        initTitle();
        tvCenterTitle.setText("退换货详情");
        id = getIntent().getStringExtra("id");
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.onlistDetail(this, id);
    }

    @Override
    public void onCanclInvite(JSONObject s) {
        ToastUtils.showToast("撤销成功");
        finish();
    }

    @Override
    public void onJh(JSONObject s) {
        ToastUtils.showToast("提交成功");
        finish();
    }

    @Override
    public void onUpReturn(JSONObject s) {

    }

    @Override
    public void onlist(CarGoodsBean s) {

    }

    @Override
    public void onlistDetail(CarGoodsDetailBean s) {
        dataItem = s;
        tvOrderOverTime.setText("申请时间: " + s.getRefund_map().getCreated());
        tvOrderNo.setText("订单号: " + s.getRefund_map().getTesq());
        tvOrderPayTime.setText("售后单号: " + s.getRefund_map().getTesq());
        mHandler.removeMessages(1);
        if (s.getRefund_map().getState() == 0) {
            line.setVisibility(View.VISIBLE);
            llVisi.setVisibility(View.VISIBLE);
            countDownSec = TimeUtils.getCountDownTime(s.getRefund_map().getEndtime(), s.getDate());
            if (countDownSec > 0) {
                //待确认,倒计时
                mHandler.sendEmptyMessage(1);
            }
            tvOrderState.setText("待商家确认");
        } else if (s.getRefund_map().getState() == 1 && s.getRefund_map().getApply_service() == 0) {
            tvOrderState.setText("待买家发货");
            llVisi.setVisibility(View.VISIBLE);
        } else if (s.getRefund_map().getState() == 1 && s.getRefund_map().getApply_service() == 1) {
            ll_wu.setVisibility(View.VISIBLE);
            tv_tj.setVisibility(View.VISIBLE);
            tvOrderState.setText("待买家发货");
        } else if (s.getRefund_map().getState() == 2 && s.getRefund_map().getApply_service() != 0) {
            tvOrderState.setText("待商家收货");
            ll_wu.setVisibility(View.VISIBLE);
            etWuName.setText(s.getRefund_map().getCompany());
            etWlNum.setText(s.getRefund_map().getNumber() + "");
            etWlNum.setEnabled(false);
            etWuName.setEnabled(false);
        } else if (s.getRefund_map().getState() == 3) {
            llVisi.setVisibility(View.VISIBLE);
            tv_order_success.setVisibility(View.VISIBLE);
            tvOrderNo.setVisibility(View.GONE);
            tvOrderState.setText("退款成功");
            tv_order_success.setText("售后单号: " + s.getRefund_map().getTesq());
            tvOrderPayTime.setText("申请时间: " + s.getRefund_map().getCreated());
            tvOrderOverTime.setText("退换时间: " + s.getRefund_map().getEndtime());
        } else if (s.getRefund_map().getState() == 4 && s.getRefund_map().getApply_service() == 0) {
            tvOrderState.setText("待平台退款");
            tvOrderNo.setVisibility(View.GONE);
            llVisi.setVisibility(View.VISIBLE);
            tv_order_success.setVisibility(View.VISIBLE);
            tvOrderOverTime.setVisibility(View.GONE);
            tv_order_success.setText("售后单号: " + s.getRefund_map().getTesq());
            tvOrderPayTime.setText("申请时间: " + s.getRefund_map().getCreated());
        } else if (s.getRefund_map().getState() == 4 && s.getRefund_map().getApply_service() == 1) {
            ll_wu.setVisibility(View.VISIBLE);
            etWuName.setText(s.getRefund_map().getCompany());
            etWlNum.setEnabled(false);
            etWuName.setEnabled(false);
            etWlNum.setText(s.getRefund_map().getNumber() + "");
            tvOrderState.setText("待平台退款");
        } else {
            tvOrderState.setText("售后关闭");
        }

        String img_zt = "";
        if (!TextUtils.isEmpty(s.getRefund_map().getZtimg())) {
            final String[] splitImgZt = s.getRefund_map().getZtimg().split(",");
            if (splitImgZt.length > 0) {
                img_zt = splitImgZt[0];
            }
        }
        GlideUtils.loadRoundImg(this, ivIcon, img_zt, R.mipmap.default_foods_icon, 4);
        tvTitle.setText(s.getRefund_map().getProduct_name());
        tvInfo.setText(s.getRefund_map().getArrribute_name());
        tvMoney.setText(getPrice(s.getRefund_map()));
        tvCount.setText("x" + s.getRefund_map().getNum() + "");
        tvMoney1.setText(getPrice(s.getRefund_map()));
        if (s.getRefund_map().getApply_service() == 0) {
            tvType.setText("仅退款");
        } else if (s.getRefund_map().getApply_service() == 1) {
            tvType.setText("退货退款 ");
        }
        switch (s.getRefund_map().getRefund_reason()) {
            case 0:
                tvVersion.setText("七天无理由退货");
                break;
            case 1:
                tvVersion.setText("质量问题");
                break;
            case 2:
                tvVersion.setText("商品描述不符");
                break;
            case 3:
                tvVersion.setText("生产日期/保质期描述不符 ");
                break;
            case 4:
                tvVersion.setText("商家发错货");
                break;
            case 5:
                tvVersion.setText("假冒品牌");
                break;
            case 6:
                tvVersion.setText("主商品破损");
                break;
            case 7:
                tvVersion.setText("其他");
                break;
        }
        tvSpeak.setText(s.getRefund_map().getInstructions());

        boolean hasImg = false;
        if (!TextUtils.isEmpty(s.getRefund_map().getImg1())) {
            hasImg = true;
            imageOne.setVisibility(View.VISIBLE);
            GlideUtils.loadImg(this, imageOne, s.getRefund_map().getImg1());
        }
        if (!TextUtils.isEmpty(s.getRefund_map().getImg2())) {
            hasImg = true;
            imageTwo.setVisibility(View.VISIBLE);
            GlideUtils.loadImg(this, imageTwo, s.getRefund_map().getImg2());
        }
        if (!TextUtils.isEmpty(s.getRefund_map().getImg3())) {
            hasImg = true;
            imageThree.setVisibility(View.VISIBLE);
            GlideUtils.loadImg(this, imageThree, s.getRefund_map().getImg3());
        }
        if (!hasImg) {
            tvReturnVoucher.setText("无");
        }
    }

    private String getPrice(CarGoodsDetailBean.RefundMapBean item) {
        String price = "";
        if (!TextUtils.isEmpty(item.getAmount_zt()) && Double.parseDouble(item.getAmount_zt()) != 0.00) {
            price += "¥";
            price += Utils.decimalUtils(item.getAmount_zt(), 2);
        }
        if (!TextUtils.isEmpty(item.getAmount_xf()) && Double.parseDouble(item.getAmount_xf()) != 0.00) {
            if (!TextUtils.isEmpty(price)) {
                price += "+";
                price += Utils.decimalUtils(item.getAmount_xf(), 2);
                price += "积分";
            } else {
                price += Utils.decimalUtils(item.getAmount_xf(), 2);
                price += "积分";
            }
        }
        if (TextUtils.isEmpty(price)) {
            price = "¥0.00";
        }
        if (item.getProducttype() == 1) {
            return price.replace("¥", "") + "欧花";

        }
        return price;
    }

    @Override
    public void onShDetail(CarGoodsDetailBean s) {

    }

    @OnClick({R.id.tv_tj, R.id.btn_cancl, R.id.btn_change, R.id.image_one, R.id.image_two, R.id.image_three})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_tj:
                String wl = etWuName.getText().toString();
                String num = etWlNum.getText().toString();
                if (TextUtils.isEmpty(wl)) {
                    ToastUtils.showToast("请输入物流");
                    return;
                }
                if (TextUtils.isEmpty(num)) {
                    ToastUtils.showToast("请输入物流单号");
                    return;

                }
                mPresenter.onJu(this, id, wl, num);
                break;
            case R.id.btn_cancl:
                final BaseDialog dialogConfirm = new BaseDialog(this, R.layout.dialog_normal, Gravity.CENTER, true);
                ((TextView) dialogConfirm.getView(R.id.tv_dialog_content)).setText("确定撤销售后退款？");
                ((TextView) dialogConfirm.getView(R.id.tv_dialog_title)).setText("撤销退款");
                dialogConfirm.getView(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialogConfirm.toggleDialog();
                    }
                });

                dialogConfirm.getView(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPresenter.onCanclInvite(ReturnGoodsListDetailsActivity.this, dataItem.getRefund_map().getTesq());
                        dialogConfirm.toggleDialog();
                    }
                });
                dialogConfirm.toggleDialog();
                break;
            case R.id.btn_change:
                ReturnGoodsDetailsActivity.start(this, dataItem.getRefund_map().getAmount_zt(), dataItem.getRefund_map().getAmount_xf(), dataItem.getRefund_map().getNum() + "", dataItem.getRefund_map().getTesq(), 2, Constants.TransCode.TransCode10039,
                        dataItem.getRefund_map().getApply_service(), dataItem.getRefund_map().getRefund_reason(), dataItem.getRefund_map().getImg1(), dataItem.getRefund_map().getImg2(), dataItem.getRefund_map().getImg3(), dataItem.getRefund_map().getInstructions(), 2, dataItem.getRefund_map().getProducttype());
                break;
            case R.id.image_one:
                List<String> imgList = new ArrayList<>();
                imgList.add(dataItem.getRefund_map().getImg1());
                imgList.add(dataItem.getRefund_map().getImg2());
                imgList.add(dataItem.getRefund_map().getImg3());
                PhotoScanActivity.startPhotoScan(this, imgList, 0);
                break;
            case R.id.image_two:
                List<String> imgList1 = new ArrayList<>();
                imgList1.add(dataItem.getRefund_map().getImg1());
                imgList1.add(dataItem.getRefund_map().getImg2());
                imgList1.add(dataItem.getRefund_map().getImg3());
                PhotoScanActivity.startPhotoScan(this, imgList1, 1);
                break;
            case R.id.image_three:
                List<String> imgList2 = new ArrayList<>();
                imgList2.add(dataItem.getRefund_map().getImg1());
                imgList2.add(dataItem.getRefund_map().getImg2());
                imgList2.add(dataItem.getRefund_map().getImg3());
                PhotoScanActivity.startPhotoScan(this, imgList2, 2);
                break;
        }
    }
}
